package com.neusoft.elm.dao.impl;

import com.neusoft.elm.dao.BusinessDao;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BusinessDaoImpl implements BusinessDao {
    Connection conn=null;
    PreparedStatement prep=null;
    ResultSet rs=null;
    @Override
    public List<Business> listBusinessByOrderTypeId(Integer orderTypeId) throws Exception {
        try {
            List<Business> list = new ArrayList<>();
            conn = DBUtil.getConnection();
            String sql = "select * from business where orderTypeId=? order by businessId";
            prep = conn.prepareStatement(sql);
            prep.setInt(1,orderTypeId);
            rs=prep.executeQuery();
            while(rs.next()){
                Business business=new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setRemarks(rs.getString("remarks"));
                list.add(business);
            }
            return  list;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            DBUtil.close(rs,prep);
        }
    }

    @Override
    public Business getBusinessById(Integer businessId) throws Exception {
        try {
            Business business=null;
            conn=DBUtil.getConnection();
            String sql="select * from business where businessId=?";
            prep=conn.prepareStatement(sql);
            prep.setInt(1,businessId);
            rs=prep.executeQuery();
            if(rs.next()){
                business=new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setRemarks(rs.getString("remarks"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setBusinessAddress(rs.getString("businessAddress"));
            }
            return business;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            DBUtil.close(rs,prep);
        }

    }
}
